<template>
	<view>
		<cu-navbar :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">创建相册</block>
		</cu-navbar>
		<view class="">
			<view class="cu-form-group margin-top">
				<view class="title">标题</view>
				<input placeholder="请输入标题" v-model="title"></input>
			</view>			
			<block v-for="(res,i) in len" :key="i">
				<view class="cu-bar bg-white margin-top">
					<view class="action">
						图片上传【{{res}}】
					</view>
					<view class="action">
						{{imgList[i].length}}/{{num}}
					</view>
				</view>
				<view class="cu-form-group">
					<view class="grid col-4 grid-square flex-sub">
						<view class="bg-img" v-for="(item,index) in imgList[i]" :key="index" @tap="ViewImage(i,index)">
							<image :src="item" mode="aspectFill"></image>
							<view class="cu-tag bg-red" @tap.stop="DelImg(i,index)">
								<text class='cuIcon-close'></text>
							</view>
						</view>
						<view class="solids" @tap="ChooseImage(i)" v-if="imgList[i].length<num">
							<text class='cuIcon-cameraadd'></text>
						</view>
					</view>
				</view>
			</block>
			<view class="padding-lg">
				<view class="padding-lg flex flex-direction">
					<button class="cu-btn bg-gradual-pink margin-tb-sm" @click="submit">提交</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		onLoad(e) {
			this.id = e.id || '';
			let len = Number(e.len) || 1;
			for(let i=0;i<len;i++){
				this.imgList.push([]);
			}
			this.len = len;
			this.num = Number(e.num) || 6;
		},
		data() {
			return {
				id:'',
				len:0,
				num:0,
				imgList: [],
				title:''
			}
		},
		methods: {
			ChooseImage(i) {
				uni.chooseImage({
					count: this.num, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {						
						res.tempFilePaths.forEach((item,index)=>{
							let file = res.tempFiles[index];
							this.$api.upload({
								name:file.name || item,
								size:file.size,
								type:file.type,
								path:item
							}).then(res=>{								
								this.imgList[i].push(res)
							})
						})
					}
				});
			},
			ViewImage(i,index) {
				uni.previewImage({
					urls: this.imgList[i],
					current: index
				});
			},
			DelImg(i,index) {
				uni.showModal({
					title: '删除图片',
					content: '确定要删除图片吗？',
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							this.imgList[i].splice(index, 1)
						}
					}
				})
			},
			submit(){
				for(let i=0;i<this.len;i++){
					if(this.num>this.imgList[i].length){
						this.$cu.toast('请上传完整图片');
						return;
					}
				}
				if(!this.title){
					this.$cu.toast('请填写标题');
					return;
				}
				this.$api.photosCreate({
					title:this.title,
					images:this.len<=1?this.imgList[0]:this.imgList,
					id:this.id
				}).then(({code,data,message})=>{
					if(!code){
						uni.redirectTo({
							url:'/pages/my/photos'
						})
					}else{
						this.$cu.toast(message)
					}
				})
			}
		}
	}
</script>

<style>

</style>
